package com.smarty.net;

import io.netty.handler.ssl.SslHandler;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;

public class ServerSll {
    public static void main(String[] args) throws IOException {
        System.out.println(ServerSll.class.getClassLoader().getResource("./test.microtf.com.jks").getPath());
        System.setProperty("javax.net.debug", "SSL,handshake");
        System.setProperty("javax.net.ssl.keyStore", ServerSll.class.getClassLoader().getResource("./test.microtf.com.jks").getPath());
        System.setProperty("javax.net.ssl.keyStorePassword", "94je6020a40fko");

        try {
            SSLServerSocketFactory socketFactory = SSLContext.getDefault().getServerSocketFactory();
            SSLServerSocket socket = (SSLServerSocket)socketFactory.createServerSocket(443);
            while (true){
                Socket s=socket.accept();
                InputStream inputStream = s.getInputStream();
                ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
                byte[] b=new byte[100];
                int i;
                while ((i=inputStream.read(b))!=-1){
                    byteArrayOutputStream.write(b,0,i);
                }
                inputStream.close();
                System.out.println(byteArrayOutputStream);

                OutputStream outputStream = s.getOutputStream();

                outputStream.write("HTTP/1.1 200 OK\r\n".getBytes(StandardCharsets.UTF_8));
                outputStream.write("\r\n\r\n".getBytes(StandardCharsets.UTF_8));
                outputStream.write("HHHHHH".getBytes(StandardCharsets.UTF_8));
                outputStream.flush();
                s.close();
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

    }
}
